package com.hyg.lib_music.ui.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.RadioGroup;

import com.hyg.lib_base.mainUtils.StatusBarUtil;
import com.hyg.lib_common.Base.BaseActivity;
import com.hyg.lib_music.R;
import com.hyg.lib_music.databinding.ActivityMusicKnowledgeBinding;
import com.hyg.lib_music.ui.fragment.KnowledgeSort1Fragment;
import com.hyg.lib_music.ui.fragment.KnowledgeSort2Fragment;
import com.hyg.lib_music.ui.fragment.KnowledgeSort3Fragment;
import com.hyg.lib_music.ui.fragment.RecommendJieyaFragment;
import com.hyg.lib_music.ui.fragment.RecommendShuimianFragment;
import com.hyg.lib_music.ui.fragment.RecommendYangshengFragment;

public class MusicKnowledgeActivity extends BaseActivity {
    ActivityMusicKnowledgeBinding binding;

    KnowledgeSort1Fragment fragment1;
    KnowledgeSort2Fragment fragment2;
    KnowledgeSort3Fragment fragment3;
    FragmentManager fragmentManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMusicKnowledgeBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        init();
    }

    public void init() {
        StatusBarUtil.setStatusBar(1, this, getResources().getColor(R.color.white), 60, null, null, true);
        binding.lnTitleView.tvTitle.setText("音乐知识");
        showMusciRecommend();
        showFragment(fragment1);
        initListener();
        binding.rbtnYangshen.performClick();
    }

    public void showMusciRecommend() {
        fragmentManager = getSupportFragmentManager();
        fragment1 = new KnowledgeSort1Fragment();
        fragment2 = new KnowledgeSort2Fragment();
        fragment3 = new KnowledgeSort3Fragment();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.add(R.id.ln_fragment_container, fragment1);
        transaction.add(R.id.ln_fragment_container, fragment2);
        transaction.add(R.id.ln_fragment_container, fragment3);
        transaction.commit();
    }


    //隐藏所有的fragment
    private void hideFragment(FragmentTransaction transaction) {
        if (fragment1 != null) {
            transaction.hide(fragment1);
        }
        if (fragment2 != null) {
            transaction.hide(fragment2);
        }
        if (fragment3 != null) {
            transaction.hide(fragment3);
        }
    }

    public void showFragment(Fragment fragment) {
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        hideFragment(transaction);
        transaction.show(fragment);
        transaction.commit();
    }

    public void initListener() {
        binding.lnTitleView.back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        binding.rbtnYangshen.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) showFragment(fragment1);
            }
        });
        binding.rbtnZhiliao.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) showFragment(fragment2);
            }
        });
        binding.rbtnTigan.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) showFragment(fragment3);
            }
        });


    }


}